﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;

#region "Custom namespace declarations"
using Comkarl;

#if !NETCF
using System.Linq.Expressions;
#endif

#endregion

namespace Comkarl.Architecture.Patterns.MVC.Entities
{
    /// <summary>
    /// Represents an action requested by a view.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for the indentifier's name.
    /// </typeparam>
    public abstract class Action<TIdentifierName> : ControlledEntity<TIdentifierName> 
        where TIdentifierName : IEquatable<TIdentifierName>
    {
        #region "Constructor"
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="id">
        /// Mandatorily sets the unique identifier for the newly initialized action.
        /// </param>
        public Action(ActionIdentifier<TIdentifierName> id)
            : base(id)
        {
        }
        #endregion

        #region "Properties"
        /// <summary>
        /// Gets the identifier for the current action.
        /// </summary>
        new public ActionIdentifier<TIdentifierName> ID
        {
            get
            {
                return (ActionIdentifier<TIdentifierName>)base.ID;
            }
        }
        #endregion

        #region "Abstract methods"
        /// <summary>
        /// Executes the current action.
        /// </summary>
        public abstract object Do();
        #endregion

        /// <summary>
        /// Converts a run-time delegate into a MVC action.
        /// </summary>
        /// <param name="handler">
        /// The function.
        /// </param>
        /// <returns>
        /// A MVC action.
        /// </returns>
        public static implicit operator Comkarl.Architecture.Patterns.MVC.Entities.Action<TIdentifierName>(RuntimeActionDelegate handler)
        {
            return new RuntimeAction<TIdentifierName, object>(handler);
        }

        #if !NETCF
        /// <summary>
        /// Converts a lambda expression into a MVC action.
        /// </summary>
        /// <param name="handler">
        /// The function.
        /// </param>
        /// <returns>
        /// A MVC action.
        /// </returns>
        public static implicit operator Comkarl.Architecture.Patterns.MVC.Entities.Action<TIdentifierName>(Expression<RuntimeActionDelegate> handler)
        {
            return new RuntimeAction<TIdentifierName, object>(new RuntimeActionDelegate(handler.Compile()));
        }
        #endif
    }
}